package cn.edu.nks.dormitory.student.register.dbimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import org.apache.log4j.Logger;
import cn.edu.nks.dormitory.model.UserInfo;
import cn.edu.nks.dormitory.student.register.db.IDBService;

import cn.edu.nks.util.DBConnectProvider;
import cn.edu.nks.util.DateTimeUtil;

public class DBService implements IDBService{
	private static Logger logger = Logger.getLogger(DBService.class);
	
	public boolean addUser(UserInfo user) {
		Connection con = null;
		PreparedStatement stmt = null;
		boolean flag = false;
		
		con = DBConnectProvider.getConnection();
		if(con == null){
			return false;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("insert into userinfo (roleid,username,password,realname,usercode,email,telephone,jsons,time) ");
		sql.append(" values(?,?,?,?,?,?,?,?,?)");
		try {
			stmt = con.prepareStatement(sql.toString());
			stmt.setInt(1, user.getRoleId());
			stmt.setString(2, user.getUserName());
			stmt.setString(3, user.getPassWord());
			stmt.setString(4, user.getRealName());
			stmt.setString(5, UUID.randomUUID().toString());
			stmt.setString(6, user.getEmail());
			stmt.setString(7, user.getTelephone());
			stmt.setString(8, user.getJsons());
			stmt.setString(9, DateTimeUtil.toDateTimeString(user.getTime()));
			flag = stmt.executeUpdate() > 0;		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBConnectProvider.freeConnection(con);
		return flag;
	}

	public UserInfo validateRepeat(String userName) {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		UserInfo item = null;
		
		con = DBConnectProvider.getConnection();
		if(null == con){
			logger.warn("get connect faild");
			return null;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("select * from userinfo where username = ?");
		try {
			stmt = con.prepareStatement(sql.toString());
			stmt.setString(1, userName);
			rs = stmt.executeQuery();
			if(rs.next()){
				item = new UserInfo();
				item.setUserName(rs.getString("username"));
				item.setUserId(rs.getInt("userid"));
			}
		} catch (SQLException e) {
			logger.warn(String.format("validateRepeat is error: %s", e));
		}
		
		DBConnectProvider.freeConnection(con);
		
		return item;
	}

	public void relateUserAndStudent(int userId) {
		Connection con = null;
		PreparedStatement stmt = null;
		
		con = DBConnectProvider.getConnection();
		if(null == con){
			logger.warn("get connect faild");
		}
		StringBuffer sql = new StringBuffer();
		sql.append("insert into studentinfo(userid,studentcode) values(?,?)");
		try {
			stmt = con.prepareStatement(sql.toString());
			stmt.setInt(1, userId);
			stmt.setString(2, UUID.randomUUID().toString());
			stmt.executeUpdate();
		} catch (SQLException e) {
			logger.warn(String.format("relateUserAndStudent is error :%s", e));
		}
		
		DBConnectProvider.freeConnection(con);
	}


}
